package com.neusoft.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neusoft.model.AppointmentsModel;
import com.neusoft.model.entity.Admins;
import com.neusoft.model.entity.User;
import com.neusoft.service.AdminService;
import com.neusoft.service.AppointmentService;
import com.neusoft.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	UserService userService;
	@Autowired
	AdminService adminService;
	@Autowired
	AppointmentService appointmentService;
	
	@RequestMapping("/tologin")
public String toLogin(){
		
		return "test_login";
	}
	
	@RequestMapping("/toreg")
	public String toreg(){
		return "test_reg";
	}
	
	@RequestMapping("/login")
	public String testLogin(String id,String password,HttpSession session,Model model){
		if(userService.testUser(id,password)||adminService.testAdmins(id,password)){
			List<User> userlList=new ArrayList<User>();
			List<Admins> adminList =new ArrayList<Admins>();
 			userlList=userService.findUserByIDAndPassword(id, password);
			adminList=adminService.findAdminByIDAndPwd(id, password);
			if(userlList.size()!=0){
			  session.setAttribute("user", userlList.get(0));
			  
			}
			if(adminList.size()!=0){
			  session.setAttribute("user", adminList.get(0));	
			}
			return "mylibrary";
			
		}	
		else {
			model.addAttribute("mes", "用户不存在");
			return "fail";
		}		
	}
	
	
	
	@RequestMapping("/admin_login")
	public String admin_login(String id,String password,HttpSession session,Model model){
		if(adminService.testAdmins(id,password)){
//			List<User> userlList=new ArrayList<User>();
			List<Admins> adminList =new ArrayList<Admins>();
// 			userlList=userService.findUserByIDAndPassword(id, password);
			adminList=adminService.findAdminByIDAndPwd(id, password);
//			if(userlList.size()!=0){
//			  session.setAttribute("user", userlList.get(0));
//			  
//			}
			if(adminList.size()!=0){
			  session.setAttribute("user", adminList.get(0));	
			}
			return "manage";
			
		}	
		else {
			model.addAttribute("mes", "用户不存在");
			return "fail";
		}		
	}
	
	/**
	 * 
	 * @Description:注销用户 
	 * @param session
	 * @return String
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session){
//		session.setAttribute("user","");
		session.removeAttribute("user");
		return "query2";		
	}
	
	@RequestMapping("/reg")
	public String regUser(User user){
		userService.insertUser(user);
		return "regsus";
	}
	/**
	 * 
	 * @Description: 跳转到查询页面
	 * @return String
	 */
	@RequestMapping("/toquery")
	private String toQueryBooks(){	
		return "query";
	}
	
	@RequestMapping("/admintologin")
	private String admintologin(){	
		return "admin_login";
	}
	
	@RequestMapping("/admin_toreg")
	private String admintoreg(){	
		return "admin_reg";
	}
	
	@RequestMapping("/tomanage")
	private String toManage(String id,Model model){	
		List<Admins> admins=adminService.findAdminByID(id);
		if(admins.size()==0){
			model.addAttribute("mes","权限不足");
			return "fail";
		}
		return "manage";
	}
	
//	@RequestMapping("/admin_login")
//	private String admin_login(String id,String password,HttpSession session,Model model){
//		if(userService.testUser(id,password)||adminService.testAdmins(id,password)){
//			List<User> userlList=new ArrayList<User>();
//			List<Admins> adminList =new ArrayList<Admins>();
// 			userlList=userService.findUserByIDAndPassword(id, password);
//			adminList=adminService.findAdminByIDAndPwd(id, password);
//			if(userlList.size()!=0){
//			  session.setAttribute("user", userlList.get(0));
//			  
//			}
//			if(adminList.size()!=0){
//			  session.setAttribute("user", adminList.get(0));	
//			}
//			return "mylibrary";
//			
//		}	
//		else {
//			model.addAttribute("mes", "用户不存在");
//			return "fail";
//		}	
//	}
	
	
	
	
	/**
	 * 
	 * @Description: 跳转到查询页面
	 * @return String
	 */
	@RequestMapping("/toquery2")
	private String toQueryBooks2(){	
		return "query2";
	}
	
	//已修改
	@RequestMapping("/appointmentInfo")
	private String appointmentInfo(String id,Model model){
		
		AppointmentsModel appointmentsModel=appointmentService.findAppointmentByUserID(id);
//		System.out.println(appointmentsModel.getRows().get(0).getAuthor()+"ff");
//		List<AppointmentsModel> appointmentsModelTmp=new ArrayList<AppointmentsModel>();
//		for(int i=0;i<appointmentsModel.getRows().size();i++){
//			AppointmentsModel appointmentsModel2=new AppointmentsModel();
//			appointmentsModel2.setCallno(appointmentsModel.getCallno());
//			appointmentsModel2.setAuthor(appointmentsModel.getAuthor());
//			appointmentsModel2.setBookName(appointmentsModel.getBookName());
//			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//			appointmentsModel2.setDeadlineString(format.format(appointmentsModel.getDeadline()));
//			appointmentsModel2.setAppointmentString(format.format(appointmentsModel.getAppointmentTime()));
//			appointmentsModelTmp.add(appointmentsModel2);
//		}
			
		model.addAttribute("appointmentInfo", appointmentsModel.getRows());
		return "appointmentInfo";
	}
	
	
	
	@RequestMapping("/toinsert")
	private String toInsert(){		
		return "redirect:/books/toinsert.action";
	}
	
	@RequestMapping("/toedit")
	private String toEdit(int id){
		
		return "redirect:/books/toedit.action?id="+id;
	}
}
